package com.hafu.service;

import com.hafu.dao.CourseDao;
import com.hafu.model.Course;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CourseDAO {
    private static final String URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String USER = "your_username";
    private static final String PASSWORD = "your_password";

    // 数据库连接工具方法
    private Connection getConnection() throws SQLException {
        try {
            // 加载 MySQL JDBC 驱动（MySQL 8.0+ 可省略，自动加载）
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "MySQL 驱动加载失败", e);
            throw new SQLException("驱动加载失败", e);
        }
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }

    // 创建课程
    public boolean createCourse(Course course) throws SQLException {
        String sql = "INSERT INTO course (course_id, course_name, teacher_id) VALUES (?, ?, ?)";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, course.getCourseId());
            pstmt.setString(2, course.getCourseName());
            pstmt.setString(3, course.getTeacherId());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "创建课程失败", e);
            throw e;
        }
    }

    // 获取学生已选课程
    public List<Course> getCoursesByStudentId(String studentId) {
        CourseDao courseDao = new CourseDao();
        try {
            return courseDao.getCoursesByStudentId(studentId);
        } catch (Exception e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "获取学生课程失败", e);
            // 兜底空集合，避免返回 null
            return new ArrayList<>();
        }
    }

    // 获取所有已发布课程
    public List<Course> getAllPublishedCourses() {
        CourseDao courseDao = new CourseDao();
        try {
            return courseDao.getAllPublishedCourses();
        } catch (Exception e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "获取已发布课程失败", e);
            return new ArrayList<>();
        }
    }

    // 删除课程
    public boolean deleteCourse(String courseId) {
        CourseDao courseDao = new CourseDao();
        try {
            return courseDao.deleteCourse(courseId);
        } catch (Exception e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "删除课程失败", e);
            return false;
        }
    }

    // 审核课程（修改状态）
    public boolean auditCourse(String courseId, String auditResult) {
        CourseDao courseDao = new CourseDao();
        try {
            return courseDao.updateCourseStatus(courseId, auditResult);
        } catch (Exception e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "审核课程失败", e);
            return false;
        }
    }

    // 搜索课程（学生视角，带关键词）
    public List<Course> searchCourses(String keyword) {
        CourseDao courseDao = new CourseDao();
        try {
            return courseDao.searchAvailableCoursesForStudent(null, keyword);
        } catch (Exception e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "搜索课程失败", e);
            return new ArrayList<>();
        }
    }

    // 获取所有课程
    public List<Course> getAllCourses() {
        CourseDao courseDao = new CourseDao();
        try {
            return courseDao.getAllCourses();
        } catch (Exception e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "获取所有课程失败", e);
            return new ArrayList<>();
        }
    }

    // 获取学生可选课程（修复静态调用问题）
    public List<Course> getAvailableCoursesForStudent(String studentId) {
        CourseDao courseDao = new CourseDao();
        try {
            // 调用非静态方法，需通过实例
            return courseDao.getAvailableCoursesForStudent(studentId);
        } catch (Exception e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "获取可选课程失败", e);
            return new ArrayList<>();
        }
    }

    // 搜索学生可选课程（修复静态调用问题）
    public List<Course> searchAvailableCoursesForStudent(String studentId, String keyword) {
        CourseDao courseDao = new CourseDao();
        try {
            // 调用非静态方法，需通过实例
            return courseDao.searchAvailableCoursesForStudent(studentId, keyword);
        } catch (Exception e) {
            Logger.getLogger(CourseDAO.class.getName()).log(Level.SEVERE, "搜索可选课程失败", e);
            return new ArrayList<>();
        }
    }
}